# -*- coding: utf-8 -*-
import sys

from PyQt5.QtCore import QObject, Qt, QPropertyAnimation, pyqtProperty, pyqtSignal
from PyQt5.QtGui import QPixmap, QPen, QPainter, QFont
from PyQt5.QtWidgets import QGraphicsPixmapItem, \
    QGraphicsSimpleTextItem, QGraphicsItem, QGraphicsView, QGraphicsScene, QFrame, QApplication
import cgitb

cgitb.enable(format='text')  # 解决pyqt5异常只要进入事件循环,程序就崩溃,而没有任何提示


class Rotation(QObject):
    def __init__(self):
        super().__init__()
        self.anim = None
        pixmap = QPixmap(r"./resources/icons/arrow.png")
        scaledPixmap = pixmap.scaled(640, 640)  # 动画大小
        self.animation()
        self.pixmap_item = QGraphicsPixmapItem(scaledPixmap)
        self.pixmap_item.setTransformOriginPoint(340, 260)  # 设置中心为旋转

    def _set_rotation(self, degree):
        self.pixmap_item.setRotation(degree)  # 自身改变旋转度数

    def animation(self):
        self.anim = QPropertyAnimation(self, b'rotation')  # 动画类型
        self.anim.setDuration(1000)
        self.anim.setStartValue(0)  # 初始角度
        self.anim.setEndValue(360)
        self.anim.setLoopCount(-1)  # 设置循环旋转

    rotation = pyqtProperty(int, fset=_set_rotation)  # 属性动画改变自身数值


class GyroWidget(QGraphicsView):
    gyroResultSignal = pyqtSignal(int)

    def __init__(self, parent=None):
        super(GyroWidget, self).__init__(parent)

        self.rota = Rotation()
        self.graphicsScene = QGraphicsScene(self)
        self.graphicsScene.setSceneRect(0, 0, 680, 520)  # 动画 位置 这个需计算
        self.graphicsScene.addItem(self.rota.pixmap_item)
        # 显示文字
        self.text_item = QGraphicsSimpleTextItem('0')
        self.text_item.setTransformOriginPoint(340, 260)  # 设置中心为旋转
        self.text_item.setPen(QPen(Qt.red))
        self.text_item.setFlags(QGraphicsItem.ItemIsSelectable | QGraphicsItem.ItemIsMovable)

        self.graphicsScene.addItem(self.text_item)
        self.setScene(self.graphicsScene)

        # 角度记录
        self.maxDegree = None
        self.minDegree = None

        # 通过图标
        self.pixmapPass = QGraphicsPixmapItem(QPixmap(r"./resources/icons/pass.png"))
        self.pixmapPass.setScale(0.3)
        self.pixmapPass.setZValue(1)
        self.pixmapPass.setPos(680 / 2, 520 / 2 - 100)
        self.pixmapPass.setVisible(False)
        self.graphicsScene.addItem(self.pixmapPass)

        # 首页
        self.pixmapHome = QGraphicsPixmapItem(QPixmap(r"./resources/icons/home.png"))
        self.pixmapHome.setScale(0.5)
        self.pixmapHome.setZValue(1)
        self.pixmapHome.setPos(680 / 2, 520 / 2 + 100)
        self.pixmapHome.setVisible(False)
        self.pixmapHome.myName = 'home'
        self.graphicsScene.addItem(self.pixmapHome)

        self.setFrameStyle(QFrame.NoFrame)  # 无Frame边框
        # 隐藏滚动条
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        # 以鼠标所在位置为锚点进行缩放
        self.setTransformationAnchor(self.AnchorUnderMouse)

        # 平滑缩放
        # self.pixmapItem.setTransformationMode(Qt.SmoothTransformation)
        self.setRenderHints(QPainter.Antialiasing |
                            QPainter.SmoothPixmapTransform)

    def gyroDataHandle(self, gyro: int):
        myDegree = gyro / 2 - 100
        print(myDegree)
        if self.maxDegree is None or myDegree > self.maxDegree:
            self.maxDegree = myDegree
        if self.minDegree is None or myDegree < self.minDegree:
            self.minDegree = myDegree
        self.rota._set_rotation(-myDegree)
        self.text_item.setRotation(-myDegree)
        self.text_item.setText(str(myDegree))
        if self.maxDegree - self.minDegree > 100:
            if not self.pixmapPass.isVisible():
                self.pixmapPass.setVisible(True)
                self.gyroResultSignal.emit(0)

    def setVisible(self, visible: bool) -> None:
        super(GyroWidget, self).setVisible(visible)
        self.pixmapPass.setVisible(False)
        self.maxDegree = None
        self.minDegree = None


# 程序入口
if __name__ == "__main__":
    # 使用下面的方式一定程度上可以解决界面模糊问题--解决电脑缩放比例问题
    # debain 上面没有这个 ：setHighDpiScaleFactorRoundingPolicy
    # QGuiApplication.setHighDpiScaleFactorRoundingPolicy(Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
    # QGuiApplication.setHighDpiScaleFactorRoundingPolicy(Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
    # 在主函数入口之前加入上面的设置即可解决
    # 适应高DPI设备
    # QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
    # 解决图片在不同分辨率显示模糊问题
    QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps)
    # QApplication.setAttribute( Qt.AA_Use96Dpi)
    app = QApplication(sys.argv)
    font = QFont("宋体")
    point_size = font.pointSize()
    font.setPixelSize(int(point_size * 90 / 72))
    app.setFont(font)
    # myWin = MyMainWindow()
    ex = GyroWidget()
    ex.show()
    sys.exit(app.exec_())
